<template>
  <GcTable
    v-loading="loading"
    :pagination="pagination"
    :columns="FEEDBACK_MANAGE_COLLUMNS"
    :tableData="tableData"
    :showFilter="false"
    :showSearch="false"
    @current-change="handlerCurrentChange">
    <template #operate="{ row }">
      <div class="operate-btn-group center">
        <gc-icon-button
          v-permission="'feedback-manage-permission:btn:delete'"
          type="link"
          @click="deleteFeedback(row.id)">删除</gc-icon-button>
      </div>
    </template>
  </GcTable>
</template>

<script setup lang="ts" name="FeedbackManage">
import { ref } from 'vue'
import { FEEDBACK_MANAGE_COLLUMNS } from './const'
import usePagination from '@/hooks/usePagination'
import type { Feedback } from '@/http/type'
import type { Pagination } from '@/@type'
import { apiGetFeedbackList, apiDeleteFeedback } from '@/http/service'
import { ElMessageBox, ElMessage } from 'element-plus'

interface ResponseFeedback extends Pagination {
  dataList: Array<Feedback>
}
const { pagination, loading } = usePagination()
const tableData = ref<Array<Feedback>>([])

/**
 * 反馈列表查询
 */
async function findFeedbackList () {
  const searchParams = {
    pageNo: pagination.pageNo,
    pageSize: pagination.pageSize
  }
  loading.value = true
  try {
    const { dataList, ...pageInfo } = await apiGetFeedbackList<ResponseFeedback>(searchParams)
    tableData.value = dataList
    Object.assign(pagination, pageInfo)
  } catch (error) {
    tableData.value = []
  } finally {
    loading.value = false
  }
}
findFeedbackList()

async function deleteFeedback (id: number) {
  await ElMessageBox.confirm(
    '删除反馈信息后将无法恢复，确认删除？',
    '提示',
    { type: 'warning' }
  )
  await apiDeleteFeedback(id)
  ElMessage.success('删除反馈信息成功')
  findFeedbackList()
}

function handlerCurrentChange (val: number) {
  pagination.pageNo = val
  findFeedbackList()
}
</script>

<style lang="scss" scoped></style>
